﻿using System;
using System.Collections.Generic;
using System.Linq;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Mechanical;
using Autodesk.Revit.DB.Plumbing;
using YArchitech.Revit;

namespace YArchitech.HVAC
{
	public class LayerBrushSetting
	{
		public Element BaseElem { get; set; }

		public Element UpdateElem { get; set; }

		public LayerBrushSetting()
		{
		}

		public LayerBrushSetting(Element elem, Element otherElem)
		{
			this.BaseElem = elem;
			this.UpdateElem = otherElem;
		}

		public bool CompareCategory()
		{
			bool result = false;
			YJKElement hyelement = new YJKElement(this.BaseElem);
			YJKElement hyelement2 = new YJKElement(this.UpdateElem);
			if (hyelement.GetElemCategoryId() == hyelement2.GetElemCategoryId())
			{
				result = true;
			}
			return result;
		}

		public void SetParameter(string paraName)
		{
			Parameter parameter = this.BaseElem.GetParameter(paraName);
			if (parameter != null && parameter.HasValue)
			{
				Parameter parameter2 = this.UpdateElem.GetParameter(paraName);
				if (parameter2 != null && !parameter2.IsReadOnly)
				{
					switch (parameter.StorageType)
					{
					case 0:
						break;
					case 1:
						parameter2.Set(parameter.AsInteger());
						return;
					case 2:
						parameter2.Set(parameter.AsDouble());
						return;
					case 3:
						parameter2.Set(parameter.AsString());
						break;
					case 4:
						parameter2.Set(parameter.AsElementId());
						return;
					default:
						return;
					}
				}
			}
		}

		public void SetParameter(string paraName, object strValue)
		{
			Parameter parameter = this.BaseElem.GetParameter(paraName);
			if (parameter != null && parameter.HasValue)
			{
				Parameter parameter2 = this.UpdateElem.GetParameter(paraName);
				if (parameter2 != null && !parameter2.IsReadOnly)
				{
					switch (parameter.StorageType)
					{
					case 0:
						break;
					case 1:
						parameter2.Set(parameter.AsInteger());
						return;
					case 2:
						parameter2.Set(UnitConvert.CovertToAPI(Convert.ToDouble(strValue), (Autodesk.Revit.DB.DisplayUnitType)2));
						return;
					case 3:
						parameter2.Set(parameter.AsString());
						break;
					case 4:
						parameter2.Set((ElementId)strValue);
						return;
					default:
						return;
					}
				}
			}
		}

		private bool ExcludeParam(Parameter param)
		{
			bool result = false;
			string name = param.Definition.Name;
			if (!(name == "偏移量"))
			{
				if (!(name == "offset"))
				{
					if (!(name == "Mark"))
					{
						if (name == "标记")
						{
							result = true;
						}
					}
					else
					{
						result = true;
					}
				}
				else
				{
					result = true;
				}
			}
			else
			{
				result = true;
			}
			return result;
		}

		public void SpecialProperty()
		{
			if (this.BaseElem is Pipe && this.UpdateElem is Pipe)
			{
				Pipe pipe = this.BaseElem as Pipe;
				Pipe pipe2 = this.UpdateElem as Pipe;
				if (pipe == null || pipe2 == null)
				{
					return;
				}
				pipe2.PipeType = pipe.PipeType;
			}
			if (this.BaseElem is Duct && this.UpdateElem is Duct)
			{
				Duct duct = this.BaseElem as Duct;
				Duct duct2 = this.UpdateElem as Duct;
				if (duct == null || duct2 == null)
				{
					return;
				}
				duct2.DuctType = duct.DuctType;
			}
		}

		public void SpecialProperty(object elemID)
		{
			if (this.BaseElem is Pipe && this.UpdateElem is Pipe)
			{
				Pipe pipe = this.BaseElem as Pipe;
				Pipe pipe2 = this.UpdateElem as Pipe;
				if (pipe == null || pipe2 == null)
				{
					return;
				}
				pipe2.PipeType = (pipe.Document.GetElementById((ElementId)elemID) as PipeType);
			}
			if (this.BaseElem is Duct && this.UpdateElem is Duct)
			{
				Duct duct = this.BaseElem as Duct;
				Duct duct2 = this.UpdateElem as Duct;
				if (duct == null || duct2 == null)
				{
					return;
				}
				duct2.DuctType = (duct.Document.GetElementById((ElementId)elemID) as DuctType);
			}
		}

		public void SetElementColor(Element baseElem, IList<Element> listElem)
		{
			try
			{
				Autodesk.Revit.DB.View activeView = baseElem.Document.ActiveView;
				System.Drawing.Color elementFillColor = UnifiedModified.GetElementFillColor(activeView, baseElem);
				System.Drawing.Color elementProjectColor = UnifiedModified.GetElementProjectColor(activeView, baseElem);
				foreach (Element elem in listElem)
				{
					UnifiedModified.SetElementFillColor(activeView, elem, elementFillColor);
					UnifiedModified.SetElementProjectColor(activeView, elem, elementProjectColor);
				}
			}
			catch (Exception ex)
			{
				throw ex;
			}
		}

		public IList<string> GetParametersName(Element baseElem, IList<Element> listElem)
		{
			IList<string> list = new List<string>();
			foreach (object obj in baseElem.Parameters)
			{
				Parameter parameter = (Parameter)obj;
				IList<string> excludeName = this.GetExcludeName();
				if (parameter != null && parameter.HasValue && !parameter.IsReadOnly)
				{
					string name = parameter.Definition.Name;
					if (!excludeName.Contains(name))
					{
						foreach (string text in this.GetListElemParameters(listElem))
						{
							if (!(name != text))
							{
								if (list.Count < 1)
								{
									list.Add(text);
								}
								else if (!list.Contains(text))
								{
									list.Add(text);
								}
							}
						}
					}
				}
			}
			if (baseElem is Pipe || baseElem is Duct)
			{
				list.Add("管道类型");
			}
			list = (from c in list
			orderby c.Normalize()
			select c).ToList<string>();
			return list;
		}

		private IList<string> GetExcludeName()
		{
			return new List<string>
			{
				"Mark",
				"mark",
				"标记",
				"拆除的阶段",
				"创建的阶段"
			};
		}

		private IList<string> GetListElemParameters(IList<Element> listElem)
		{
			IList<string> list = new List<string>();
			IList<ElementId> list2 = new List<ElementId>();
			foreach (Element elem in listElem)
			{
				ElementId elemCategoryId = new YJKElement(elem).GetElemCategoryId();
				if (list2.Count < 1)
				{
					list2.Add(elemCategoryId);
				}
				else if (!list2.Contains(elemCategoryId))
				{
					list2.Add(elemCategoryId);
				}
			}
			foreach (ElementId elementId in list2)
			{
				foreach (Element element in listElem)
				{
					ElementId elemCategoryId2 = new YJKElement(element).GetElemCategoryId();
					if (!(elementId != elemCategoryId2))
					{
						foreach (object obj in element.Parameters)
						{
							Parameter parameter = (Parameter)obj;
							if (parameter != null && !parameter.IsReadOnly)
							{
								string name = parameter.Definition.Name;
								if (list.Count < 1)
								{
									list.Add(name);
								}
								else if (!list.Contains(name))
								{
									list.Add(name);
								}
							}
						}
					}
				}
			}
			return list;
		}
	}
}
